//
//  LaunchADViewController.swift
//  NightNews
//
//  Created by 薛永伟 on 2019/8/28.
//  Copyright © 2019 XueYongWei. All rights reserved.
//

import UIKit
import SnapKit

class LaunchADViewController: UIViewController {

    typealias completeHandle = ()->()
    
    var complete:completeHandle?
    
    /// 点击链接
    var clickLinkL:String?
    
    //MARK:属性
    /// 计时器
    lazy var timer: YRTimer = {
        
        let timer = YRTimer.init(interval: 1, repeats: true, repeatRun: {[weak self] in
            
            self?.cutDownTime()
        })
        return timer
    }()
    
    
    /// 当前秒数
    fileprivate var currentSeconds = 4
    
    
    fileprivate lazy var logView: SplashLogView = {
        let view = SplashLogView()
        view.clipsToBounds = true
        view.skipBtn.addTarget(self, action: #selector(onSkipClick), for: .touchUpInside)
        return view
    }()
    
    
    
    lazy var contentView: UIView = {
        let view = UIView()
        view.clipsToBounds = true
        view.backgroundColor = .white
        return view
    }()
    
    lazy var imageView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        view.clipsToBounds = true
        if let tap = UITapGestureRecognizer.init(actionBlock: { (any) in
            if let link = self.clickLinkL ,
                let url = URL.init(string: link) {
                
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
                self.onSkipClick()
            }
            
        }){
            view.isUserInteractionEnabled = true
            view.addGestureRecognizer(tap)
        }
        return view
    }()
    
    deinit {
        YRLog("LaunchADViewController deinit", type: .info)
        self.timer.close()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.timer.start()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        
        
        super.viewWillDisappear(animated)
        
        navigationController?.setNavigationBarHidden(true, animated: animated)
        self.timer.close()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(logView)
        logView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(105.scaleLength)
            make.bottom.equalToSuperview()
        }
        
        view.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(logView.snp.top).offset(0)
        }
        
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    @objc func onSkipClick(){
        complete?()
    }
    
    @objc func cutDownTime(){
        
        currentSeconds -= 1
        
        guard currentSeconds > 0 else {
            onSkipClick()
            return
        }
        
        logView.skipBtn.setTitle("跳过 \(currentSeconds)", for: .normal)
        
    }
    
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
}

extension LaunchADViewController {
    /// app进入后台
    @objc func appDidEnterBackGround(){
        self.timer.pause()
    }
    
    /// app进入前台
    @objc func appWillEnterForceGround(){
        self.timer.resume()
    }
}

class SplashLogView:CodeLayoutView {
    
    lazy var imgView: UIImageView = {
        let view = UIImageView()
        view.contentMode = .scaleAspectFit
        view.image = UIImage.init(named: "launch_botton_Logo组合")
        view.clipsToBounds = true
        return view
    }()
    
    fileprivate lazy var adLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor.init(rgb: 0xB4B4B4)
        view.textAlignment = .right
        view.font = UIFont.systemFont(ofSize: 8, weight: .regular)
        view.text = "广告"
        return view
    }()
    
    lazy var skipBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("跳过", for: .normal)
        btn.setTitleColor(UIColor.init(rgb: 0xB4B4B4), for: .normal)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 10)
        btn.layer.cornerRadius = 11
        btn.layer.borderWidth = 1.0.scaleLength
        btn.layer.borderColor = UIColor.init(rgb: 0xB4B4B4)?.cgColor
        btn.clipsToBounds = true
        return btn
    }()
    
    override func codeCustomSubviews() {
        
        addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(160)
            make.height.equalTo(54)
        }
        
        addSubview(adLabel)
        adLabel.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-8.scaleLength)
            make.top.equalToSuperview().offset(8.scaleLength)
        }
        
        addSubview(skipBtn)
        skipBtn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-15.scaleLength)
            make.right.equalToSuperview().offset(-15.scaleLength)
            make.width.equalTo(54.scaleLength)
            make.height.equalTo(22.scaleLength)
        }
    }
}


extension LaunchADViewController {
    
    struct ADModel {
        
        var image:UIImage
        var link:String
    }
}
